package com.fingard.dsp.bank.directbank.cbhb02;

import com.chinapay.secss.SecssConstants;
import com.chinapay.secss.SecssUtil;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IRealTime;
import com.fingard.dsp.bank.directbank.chinapay01.util.SDKUtil;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingle;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingle;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
import net.sf.json.JSONObject;
import org.apache.commons.codec.binary.Base64;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

public class CBHB02RealTime extends CBHB02Base implements IRealTime {
    public RetAtsSingle d_9108_RealTimeRec(ReqSingle p_req9108) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req9108.reqHeader);
        RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
        tmpRetDetail.reqSeqID = p_req9108.singleDetail.askNumber;

        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_req9108.singleDetail.recAct, "");
            if (actItem == null) {
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = "未找到账号[" + p_req9108.singleDetail.recAct + "]商户配置信息!";
                tmpRetDetail.transState = PaymentState.FAIL_Payment;
                tmpRetDetail.payInfoCode = FailCode.E9981;
                retAts.writeRetDetail(tmpRetDetail);
                return retAts;
            }

            CBHB02Header bankHeader = new CBHB02Header();
            bankHeader.transType = "SCTQ";
            bankHeader.instSeq = p_req9108.singleDetail.askNumber;
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            xmlWriter.autoFormat = false;
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeElementString("pyrAct", p_req9108.singleDetail.payAct);
            xmlWriter.writeElementString("pyrNam", p_req9108.singleDetail.payName);
            xmlWriter.writeElementString("pyeAct", p_req9108.singleDetail.recAct);
            xmlWriter.writeElementString("pyeNam", p_req9108.singleDetail.recName);
            xmlWriter.writeElementString("pyeBnk", p_req9108.singleDetail.cnapsCode);
            xmlWriter.writeElementString("amt", p_req9108.singleDetail.payAmount);
            xmlWriter.writeElementString("postscript", p_req9108.singleDetail.memo);
            xmlWriter.writeElementString("agrNo", "1");
            xmlWriter.writeElementString("feeNo", "1");
            xmlWriter.writeStartDocByDeclaration("</" + bankHeader.transType + "Req>");
            xmlWriter.writeEndElement();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/Cbhbpac/Message/"+bankHeader.transType+"Res/rspCode");
                    String tmpRetInfo = tmpBkReader.getFirstText("/Cbhbpac/Message/"+bankHeader.transType+"Res/rspMsg");
                    tmpRetDetail.respCode = "0";
                    tmpRetDetail.respInfo = tmpRetInfo;
                    tmpRetDetail.reqSeqID = p_req9108.singleDetail.askNumber;
                    tmpRetDetail.transState = tmpRetCode;
                    tmpRetDetail.payInfoCode = tmpRetCode;
                    tmpRetDetail.transAmt = p_req9108.singleDetail.payAmount;
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpSbMsg.toString();
                }
            } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = tmpStrRet[1];
                tmpRetDetail.reqSeqID = p_req9108.singleDetail.askNumber;
                tmpRetDetail.transState = PaymentState.FAIL_Payment;
                tmpRetDetail.payInfoCode = tmpStrRet[0];
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsSingle d_9144_QryRealTimeRec(ReqQrySingle p_req9144) throws Exception {
        return new CBHB02SinglePay().d_1944_QrySglPay(p_req9144);
    }
}
